package Jets.ui
{
   import Jets.ui.inface.Ilistable;
   
   import flash.display.DisplayObjectContainer;
   import flash.events.Event;
   import flash.text.AntiAliasType;
   import flash.text.TextField;
   import flash.text.TextFieldAutoSize;
   import flash.text.TextFormat;
   import Jets.display.JContainer;
   
   public class JLabel extends JContainer implements Ilistable
   {
	   private var _autoSize:Boolean = true;
	   protected var _text:String = "";
	   public var _tf:TextField;
      public function JLabel(parent:DisplayObjectContainer=null, cx:Number=0, cy:Number=0, text:String="") 
	  {
         this._text = text;
         super(parent,cx,cy);
      }
      
     
	  /**绑定字体 后期实现*/
      private var _embedFonts:String;
      public function get embedFonts() : String 
	  {
         return this._embedFonts;
      }
      public function set embedFonts(param1:String) : void 
	  {
         /*
		  if(this._embedFonts != param1)
         {
            this._embedFonts = param1;
            if((this._embedFonts) && (FontManager.fonts[param1]))
            {
               this._tf.embedFonts = true;
               this._tf.antiAliasType = AntiAliasType.ADVANCED;
               this._tf.defaultTextFormat = new TextFormat(FontManager.fonts[param1].fontName,12,16777215);
            }
            else
            {
               this._tf.embedFonts = false;
               this._tf.defaultTextFormat = new TextFormat("微软雅黑",12,16777215);
            }
         }*/
      }
      
      override protected function init() : void {
         super.init();
         mouseEnabled = false;
      }
      
      override protected function addChildren() : void {
         this._tf = new TextField();
         _width = 16;
         height = 20;
         this._tf.selectable = false;
         this._tf.mouseEnabled = false;
         this._tf.autoSize = TextFieldAutoSize.LEFT;
         this._tf.defaultTextFormat = new TextFormat("Courier New",12,16777215);
         addChild(this._tf);
      }
      
      override public function draw() : void {
         super.draw();
         if(this._autoSize)
         {
            this._tf.x = 0;
            if(this._usehtml)
            {
               this._tf.htmlText = (this._text) || "";
            }
            else
            {
               this._tf.text = (this._text) || "";
            }
            _width = this._tf.width;
         }
         else
         {
            if(this._usehtml)
            {
               this._tf.htmlText = (this._text) || "";
            }
            else
            {
               this._tf.text = (this._text) || "";
            }
            if(this.align == TextFieldAutoSize.CENTER)
            {
               this._tf.x = _width / 2 - this.textWidth / 2;
            }
            else
            {
               if(this.align == TextFieldAutoSize.RIGHT)
               {
                  this._tf.x = _width - this.textWidth;
               }
            }
         }
         this._tf.y = Math.round(_height / 2 - this._tf.height / 2);
      }
      
	  /**使用html文本格式*/
      private var _usehtml:Boolean = false;
      public function set usehtml(bool:Boolean) : void 
	  {
         if(this._usehtml != bool)
         {
            this._usehtml = bool;
         }
         this.invalidate();
      }
      public function get usehtml() : Boolean 
	  {
         return this._usehtml;
      }
      
	  /**文本对齐方式*/
      private var align:String = "left";
      public function set Align(value:String) : void 
	  {
         if(this.align != value)
         {
            this.align = value;
            if(value != TextFieldAutoSize.LEFT)
            {
               this.autoSize = false;
            }
            invalidate();
         }
      }
      public function get Align() : String 
	  {
         return this.align;
      }
      
	  public function get text() : String 
	  {
		  return this._text;
	  }
      public function set text(value:String) : void 
	  {
         if(this._text != value)
         {
            drawed = false;
            this._text = value;
            if(stage)
            {
               this.onadd(null);
            }
            else
            {
               this.addEventListener(Event.ADDED_TO_STAGE,this.onadd);
            }
         }
      }
      
      protected function onadd(event:Event) : void 
	  {
         this.removeEventListener(Event.ADDED_TO_STAGE,this.onadd);
         invalidateNow();
      }
      
     
      /**与text方式一样*/
      override public function set data(value:Object) : void {
         if(value != null)
         {
            this.text = value + "";
         }
         else
         {
            this.text = "";
         }
      }
      override public function get data() : Object 
	  {
         return this._text;
      }
	  
	  
      /**文字颜色*/
      public function set textColor(color:uint) : void 
	  {
         this._tf.textColor = color;
      }
      public function get textColor() : uint 
	  {
         return this._tf.textColor;
      }
      
	  
      public function get textSize() : Object 
	  {
		  var txtfmt:TextFormat = this._tf.defaultTextFormat;
		 if(txtfmt)
		 {
			 return txtfmt.size;
		 }
		 return null;
      }
      public function set textSize(value:Object) : void 
	  {
		  var txtfmt:TextFormat = this._tf.defaultTextFormat;
         if(!txtfmt)
         {
			 txtfmt = new TextFormat(this.textFont,null,this.textColor,this.isBold);
         }
		 txtfmt.size = value;
         this._tf.defaultTextFormat = txtfmt;
         this._tf.setTextFormat(txtfmt);
      }
      
	  
      public function get textFont() : String 
	  {
		  var txtfmt:TextFormat = this._tf.defaultTextFormat;
         if(txtfmt)
         {
            return txtfmt.font;
         }
         return null;
      }
      public function set textFont(value:String) : void 
	  {
		  var txtfmt:TextFormat = this._tf.defaultTextFormat;
         if(!txtfmt)
         {
			 txtfmt = new TextFormat(null,this.textSize,this.textColor,this.isBold);
         }
		 txtfmt.font = value;
         this._tf.defaultTextFormat = txtfmt;
         this._tf.setTextFormat(txtfmt);
      }
      
	  
      public function get isBold() : Object 
	  {
		  var txtfmt:TextFormat = this._tf.defaultTextFormat;
         if(txtfmt)
         {
            return txtfmt.bold;
         }
         return null;
      }
      public function set isBold(value:Object) : void {
		  var txtfmt:TextFormat = this._tf.defaultTextFormat;
         if(!txtfmt)
         {
			 txtfmt = new TextFormat(this.textFont,this.textSize,this.textColor);
         }
		 txtfmt.bold = value;
         this._tf.defaultTextFormat = txtfmt;
         this._tf.setTextFormat(txtfmt);
      }
      
	  /***/
      public function set autoSize(value:Boolean) : void 
	  {
         this._autoSize = value;
      }
      public function get autoSize() : Boolean 
	  {
         return this._autoSize;
      }
      
      override public function get width() : Number 
	  {
         if(drawed == false)
         {
            drawed = true;
            invalidateNow();
         }
         return _width;
      }
      override public function get height() : Number 
	  {
         if(drawed == false)
         {
            drawed = true;
            invalidateNow();
         }
         return _height;
      }
      
      public function get textWidth() : Number 
	  {
         if(drawed == false)
         {
            drawed = true;
            invalidateNow();
         }
         return this._tf.width;
      }
      public function get textHeight() : Number 
	  {
         this._tf.wordWrap = true;
         this._tf.multiline = true;
         return this._tf.textHeight;
      }
   }
}
